package com.example.echoewish.ui.my_activity.user_info_activity;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.echoewish.econstant.Constant;
import com.example.echoewish.pojo.User;
import com.example.echoewish.repository.UserMessageRepository;
import com.example.echoewish.util.LogUtil;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserInfoEditViewModel extends ViewModel {
    MutableLiveData<User>userMutableLiveData;
    MutableLiveData<String> responseMutableData;

    public UserInfoEditViewModel()
    {
        userMutableLiveData=new MutableLiveData<>();
        responseMutableData=new MutableLiveData<>();
    }

    public void updateUserInfo()
    {
        UserMessageRepository.getInstance().updateUserInfo(userMutableLiveData.getValue(),new UpdateUserInfoCallBack());
    }

    private class UpdateUserInfoCallBack implements Callback<ResponseBody>
    {

        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                responseMutableData.postValue(response.body().string());
            } catch (IOException ioException) {
                new LogUtil().w(LogUtil.CALLBACK_TAG,"更新好友信息 回调失败");
                responseMutableData.postValue(Constant.HTTPS_GET_CALLBACK_FAIL);
                ioException.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            new LogUtil().w(LogUtil.CALLBACK_TAG,"更新好友信息 回调失败"+t);
            responseMutableData.postValue(Constant.HTTPS_GET_CALLBACK_FAIL);
        }
    }
}
